package util

import (
	"io/ioutil"
)

/**
 * 获取某个路径下的所有目录
 * @param dirPth 目录路径
 * @return
 */
func GetAllDirUnderPath(dirPth string) ([]string, error) {
	var dirs []string
	dir, err := ioutil.ReadDir(dirPth)
	if err != nil {
		return nil, err
	}

	for _, fi := range dir {
		if fi.IsDir() { // 目录, 递归遍历
			dirs = append(dirs, fi.Name())
		}
	}

	return dirs, err
}

/**
 * 获取文档列表
 * @param dirPth 文档所在目录
 * @return
 */
func GetDocList(dirPth string) (map[string]any, error) {
	resultMap := make(map[string]any)
	// 获取 product
	product, err := GetAllDirUnderPath(dirPth)
	if product != nil && err == nil {
		productMap := make(map[string]any)
		resultMap[dirPth] = productMap
		// 获取 platform
		for _, productItem := range product {
			// 获取 version
			platform, err := GetAllDirUnderPath(dirPth + "/" + productItem)
			if product != nil || err == nil {
				platformMap := make(map[string]any)
				productMap[productItem] = platformMap
				// 获取 platform
				for _, platformItem := range platform {
					// 获取 version
					version, err := GetAllDirUnderPath(dirPth + "/" + productItem + "/" + platformItem)
					if product != nil || err == nil {
						platformMap[platformItem] = version
					}
				}
			}

		}
	}

	return resultMap, err
}
